﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Data;
using System.ComponentModel;
using EIA.PM.Model;

namespace EIA.SLClient.PlatManage.PMMgr
{
    public class EngineerListViewModel : SimpleMvvmToolkit.ViewModelBase<EngineerListViewModel>
	{
        public EngineerListViewModel()
        {
            this.RegisterToReceiveMessages(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.ENGINEER_RETRIEVE, EP_Retrieve);
            this.RegisterToReceiveMessages<TEngineer>(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.ENGINEER_NEW_CANCEL, EP_NewCancel);
        }

        private void EP_Retrieve(object sender, SimpleMvvmToolkit.NotificationEventArgs e)
        {
            RetrieveItems();
        }
        private void EP_NewCancel(object sender, SimpleMvvmToolkit.NotificationEventArgs<TEngineer> e)
        {
            if (e.Data != null && EngineerCollection != null) EngineerCollection.Remove(e.Data);
        }

        private void Busy(string msg = "等待...")
        {
            IsBusy = true;
            SendMessage<bool>(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.ENGINEER_BUSY_STATE, new SimpleMvvmToolkit.NotificationEventArgs<bool>(msg, true));
        }

        private void Free()
        {
            IsBusy = false;
            SendMessage<bool>(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.ENGINEER_BUSY_STATE, new SimpleMvvmToolkit.NotificationEventArgs<bool>("", false));
        }

        #region notifyprop

        protected bool _IsBusy;
        /// <summary>
        /// 
        /// </summary>
        public bool IsBusy
        {
            get { return _IsBusy; }
            set
            {
                _IsBusy = value;
                NotifyPropertyChanged(m => m.IsBusy);
            }
        }

        protected PagedCollectionView _EngineerCollection;
        public PagedCollectionView EngineerCollection
        {
            get { return _EngineerCollection; }
            set
            {
                _EngineerCollection = value;
                NotifyPropertyChanged(m => m.EngineerCollection);
            }
        }

        #endregion

        #region command

        #region RetrieveItemsCommand

        private SimpleMvvmToolkit.DelegateCommand _RetrieveItemsCommand = null;
        public SimpleMvvmToolkit.DelegateCommand RetrieveItemsCommand
        {
            get
            {
                if (_RetrieveItemsCommand == null)
                {
                    _RetrieveItemsCommand = new SimpleMvvmToolkit.DelegateCommand(RetrieveItems, CanExcuteRetrieveItems);
                } return _RetrieveItemsCommand;
            }
            set { _RetrieveItemsCommand = value; NotifyPropertyChanged(m => m.RetrieveItemsCommand); }
        }

        public void RetrieveItems()
        {
            EIA.PM.BLL.ClientProxy.TEngineerProxy proxy = new EIA.PM.BLL.ClientProxy.TEngineerProxy();
            Busy();
            proxy.ListAll((lst) =>
            {
                Free();
                EngineerCollection = new PagedCollectionView(lst);
            }, (ex) =>
            {
                Free();
                System.Windows.MessageBox.Show("获取工程师列表发生错误！", "提示", System.Windows.MessageBoxButton.OK);
            });
        }

        public bool CanExcuteRetrieveItems()
        {
            return true;
        }

        #endregion

        #region InsertItemCommand

        private SimpleMvvmToolkit.DelegateCommand _InsertItemCommand = null;
        public SimpleMvvmToolkit.DelegateCommand InsertItemCommand
        {
            get
            {
                if (_InsertItemCommand == null)
                {
                    _InsertItemCommand = new SimpleMvvmToolkit.DelegateCommand(InsertItem, CanExcuteInsertItem);
                } return _InsertItemCommand;
            }
            set { _InsertItemCommand = value; NotifyPropertyChanged(m => m.InsertItemCommand); }
        }

        public void InsertItem()
        {
            TEngineer eng = new TEngineer();

            List<TEngineer> lst = new List<TEngineer>();
            lst.Add(eng);
            lst.AddRange(EngineerCollection.SourceCollection as IList<TEngineer>);
            EngineerCollection = new PagedCollectionView(lst);

            this.SendMessage<TEngineer>(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.ENGINEER_NEW, new SimpleMvvmToolkit.NotificationEventArgs<TEngineer>("", eng));
        }

        public bool CanExcuteInsertItem()
        {
            return true;
        }

        #endregion

        #region UpdateItemCommand

        private SimpleMvvmToolkit.DelegateCommand<EIA.PM.Model.TEngineer> _UpdateItemCommand = null;
        public SimpleMvvmToolkit.DelegateCommand<EIA.PM.Model.TEngineer> UpdateItemCommand
        {
            get
            {
                if (_UpdateItemCommand == null)
                {
                    _UpdateItemCommand = new SimpleMvvmToolkit.DelegateCommand<EIA.PM.Model.TEngineer>(UpdateItem, CanExcuteUpdateItem);
                } return _UpdateItemCommand;
            }
            set { _UpdateItemCommand = value; NotifyPropertyChanged(m => m.UpdateItemCommand); }
        }

        public void UpdateItem(EIA.PM.Model.TEngineer p)
        {
            this.SendMessage<TEngineer>(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.ENGINEER_UPDATE, new SimpleMvvmToolkit.NotificationEventArgs<TEngineer>("", p));
        }

        public bool CanExcuteUpdateItem(EIA.PM.Model.TEngineer p)
        {
            return true;
        }

        #endregion

        #region DeleteItemCommand

        private SimpleMvvmToolkit.DelegateCommand<EIA.PM.Model.TEngineer> _DeleteItemCommand = null;
        public SimpleMvvmToolkit.DelegateCommand<EIA.PM.Model.TEngineer> DeleteItemCommand
        {
            get
            {
                if (_DeleteItemCommand == null)
                {
                    _DeleteItemCommand = new SimpleMvvmToolkit.DelegateCommand<EIA.PM.Model.TEngineer>(DeleteItem, CanExcuteDeleteItem);
                } return _DeleteItemCommand;
            }
            set { _DeleteItemCommand = value; NotifyPropertyChanged(m => m.DeleteItemCommand); }
        }

        public void DeleteItem(EIA.PM.Model.TEngineer p)
        {
            if (System.Windows.MessageBox.Show(string.Format("确认删除工程师 [{0}]", p.StfName), "提示", System.Windows.MessageBoxButton.OKCancel) != System.Windows.MessageBoxResult.OK) return;

            EIA.PM.BLL.ClientProxy.TEngineerProxy proxy = new EIA.PM.BLL.ClientProxy.TEngineerProxy();
            Busy();
            proxy.Delete(p.EngId, (r) =>
            {

                Free();
                if (r)
                {
                    EngineerCollection.Remove(p);
                }
                else
                {
                    // 未删除
                    System.Windows.MessageBox.Show("删除工程师发生错误！", "提示", System.Windows.MessageBoxButton.OK);
                }

            }, (ex) =>
            {
                Free();
            });
        }

        public bool CanExcuteDeleteItem(EIA.PM.Model.TEngineer p)
        {
            return true;
        }

        #endregion

        #endregion

        public void OnLoad()
        {
            if (EngineerCollection == null) RetrieveItems();
        }
	}
}